import os
import random
import sqlite3
import subprocess
import time
import tkinter as tk

AppWindow = tk.Tk()
AppWindow.title("加密大亨模拟游戏1中英文版")
screenwidth = AppWindow.winfo_screenwidth()
screenheight = AppWindow.winfo_screenheight()
width = 400
height = 185
AppWindow.geometry("%dx%d+%d+%d" % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2))
AppWindow.resizable(False, False)
label1 = tk.Label(AppWindow, text="加密大亨模拟游戏1",font=("Arial",18))
label1.pack()
frame=tk.Frame(AppWindow)
frame.pack()
def showpass():
    UserPasswordEntry.config(show='')
    showpasswordbutton.config(command=hidepass)
    showpasswordbutton.config(text="隐藏")
def hidepass():
    UserPasswordEntry.config(show='*')
    showpasswordbutton.config(command=showpass)
    showpasswordbutton.config(text="显示")

#登陆，，
def Login():
    exit_code=os.system("ping www.baidu.com")
    if exit_code == 0:
        time.sleep(1)
        print("连接数据库成功")
        conn = sqlite3.connect('C:\\Users\\whb\\Desktop\\游戏\\Python_CryptoTycoonEnglishAndChineseVersion\\database\\usernamepass.db')

        username=UserNameEntry.get()
        password =UserPasswordEntry.get()
        def showdata(username):
            data = conn.execute("select * from UsernamePassword where Username='%s'" % username).fetchone()
            print(data)
            return data
        def login(username, password):
            data = showdata(username)  # 获取用户名对应的数据库资料
            if data:
                if data[1] == password:

                    print("登录成功")

                    conn.close()
                else:
                    print("密码错误")

            else:
                print("用户名不存在")
        login(username,password)
    else:
        print("网络不良好，请重新连接网络")

#注册
def Signup():
    username=UserNameEntry.get()
    password=UserPasswordEntry.get()
    if username=="" or password == "":
        print("用户名和密码不能为空，请重新输入！")
    else:
        private_key = []
        finalpravitekey = ""
        keyletter = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
                     "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
        for i in range(len(keyletter)):
            random.shuffle(keyletter)
            print(keyletter)
        for j in range(10):  # 5字母
            letter = keyletter[random.randrange(0, len(keyletter) - 1)]
            private_key.append(letter)
            random.shuffle(keyletter)
            keyletter.remove(letter)
        for k in range(10):  # 5数字
            keynumber = random.randint(0, 100)
            private_key.append(str(keynumber))
        random.shuffle(private_key)
        for i in range(len(private_key)):
            print(private_key[i])
            finalpravitekey = private_key[i] + finalpravitekey
        print(private_key)
        print(finalpravitekey)

        conn = sqlite3.connect('C:\\Users\\whb\\Desktop\\游戏\\Python_CryptoTycoonEnglishAndChineseVersion\\database\\usernamepass.db')

        def showdate(username):
            data = conn.execute("select * from UsernamePassword where Username='%s'" % username).fetchall()
            if len(data) == 0:
                print("ok")
                sql = "INSERT INTO UsernamePassword (Username,Password,PraviteKey)VALUES (?,?,?);"
                val = [username, password, finalpravitekey]
                conn.execute(sql, val)
                conn.commit()
                conn.close()
            else:
                print("bad")

        showdate(username)
#返回
def back():
    AppWindow.destroy()
    subprocess.Popen(["python","./main.py"],shell=True)

#退出系统
def Clear():
    UserNameEntry.delete(0,tk.END)
    UserPasswordEntry.delete(0,tk.END)

UserNameLabel = tk.Label(frame, text="用户名:",font=("Arial",14))
UserNameLabel.grid(row=0,column=0)
UserNameEntry=tk.Entry(frame,font=("Arial",14))
UserNameEntry.grid(row=0,column=1)
UserPasswordLabel = tk.Label(frame, text="密码:",font=("Arial",14))
UserPasswordLabel.grid(row=1,column=0)
UserPasswordEntry=tk.Entry(frame,font=("Arial",14),show='*')
UserPasswordEntry.grid(row=1,column=1)
showpasswordbutton=tk.Button(frame,text="显示",font=("Arial",14),command=showpass)
showpasswordbutton.grid(row=1, column=2)

frame1=tk.Frame(AppWindow)
frame1.pack()
LoginButton=tk.Button(frame1,text="登录",font=("Arial",16),width=14,command=Login)
LoginButton.grid(row=0,column=0,padx=5)
SignupButton = tk.Button(frame1,text="注册",font=("Arial",16),width=14,command=Signup)
SignupButton.grid(row=0,column=1,padx=5)
BackButton = tk.Button(frame1, text="返回",font=("Arial",16),width=14,command=back)
BackButton.grid(row=1, column=0, padx=5, pady=5)
ClearButton = tk.Button(frame1, text="清空",font=("Arial",16),width=14,command=Clear)
ClearButton.grid(row=1, column=1, padx=5, pady=5)
AppWindow.mainloop()
